package com.example.toutiao.ui.main.act


import android.text.Editable
import android.text.InputFilter
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import android.widget.EditText
import android.widget.Toast
import androidx.core.content.ContextCompat
import com.example.toutiao.R
import com.example.toutiao.base.BaseActivity
import com.example.toutiao.base.Constants
import com.example.toutiao.ui.main.model.LoginModel
import com.example.toutiao.ui.main.model.RegisterModel
import com.example.toutiao.ui.main.presenter.LoginPresenter
import com.example.toutiao.ui.main.view.LoginView
import com.example.toutiao.ui.user.EvtMsgModel
import com.example.toutiao.utils.CommonUtils
import com.example.toutiao.utils.SharedPreferencesUtils
import com.example.toutiao.view.MyCheckBox
import kotlinx.android.synthetic.main.activity_pass_login.*

class PassLoginActivity : BaseActivity<LoginView, LoginPresenter>(), LoginView,
    TextWatcher {
    private var phone: String = ""
    private var password: String = ""
    override fun getLayoutId(): Int = R.layout.activity_pass_login

    override fun initView() {
        cd_next.setCardBackgroundColor(ContextCompat.getColor(this, R.color.c_222222))
        my_checkbox.isShow(false)
        my_checkbox.initSpan(R.string.xy)
        login_phone.addTextChangedListener(this)
        login_password.addTextChangedListener(this)
        my_checkbox.setClickableSpanText(object : MyCheckBox.ClickableSpanText {
            override fun onClick(type: Int) {
                if (type == 1) {
                    showToast("用户协议")
                } else {
                    showToast("隐私协议")
                }
            }
        })
        cd_next.setOnClickListener {
            if (TextUtils.isEmpty(phone)) {
                Toast.makeText(application, "请先填写电话号码", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (TextUtils.isEmpty(password)) {
                Toast.makeText(application, "请填写密码", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            getPresenter()!!.login(phone,password)
        }
        icon_back.setOnClickListener {
            finish()
        }
        /* 找回密码 */
        tv_find_pass.setOnClickListener {
            startActivity(FindPassActivity::class.java)
        }
    }

    override fun initData() {

    }

    override fun createPresenter(): LoginPresenter = LoginPresenter()


    override fun onRegister(registerModel: RegisterModel) {

    }

    override fun <T> setData(data: T) {
        if(data!=null){
            var result=data as LoginModel
            SharedPreferencesUtils.saveToken(this, Constants.TOKEN, result.token)
            postMsg(EvtMsgModel(102, result.token))
            postMsg(EvtMsgModel(201, result.token))
            finish()
        }
    }

    override fun setError(err: String) {

    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        phone = CommonUtils.replaceBlank(login_phone.text.toString())
        password = login_password.text.toString()
        check()
    }

    private fun check() {
         if(phone.contains("@")){
             login_phone.onInputFilter(20)
            arrow_down.visibility= View.GONE
            tv_86.visibility=View.GONE
        }else{
             login_phone.onInputFilter(13)
            arrow_down.visibility=View.VISIBLE
            tv_86.visibility=View.VISIBLE
        }
        if (phone.length == 11 && !TextUtils.isEmpty(password)) {
            cd_next.setCardBackgroundColor(ContextCompat.getColor(this, R.color.main_color))
            cd_next.isEnabled = true
        } else {
            cd_next.setCardBackgroundColor(ContextCompat.getColor(this, R.color.c_222222))
            cd_next.isEnabled = false
        }
    }
    private fun EditText.onInputFilter (size:Int){
        login_phone.filters= arrayOf<InputFilter>(InputFilter.LengthFilter(size))
    }

    override fun afterTextChanged(s: Editable?) {

    }

    override fun isRegister(): Boolean = true
    override fun getMessage(message: EvtMsgModel<*>) {
        super.getMessage(message)
        if (message != null) {
            if (message.code == 105) {
                finish()
            }
        }
    }
}